package com.tx.servlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.tx.service.StudentService;
import com.tx.service.impl.StudentServiceImpl;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import java.util.Map;

@WebServlet("/student/query")
public class StudentQueryServlet extends HttpServlet {
    private final StudentService studentService = new StudentServiceImpl();

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        
        try {
            List<Map<String, Object>> students = studentService.getAllStudentsWithMajor();
            outputStudentTable(students, out);
        } catch (Exception e) {
            e.printStackTrace();
            response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            out.println("查询失败：" + e.getMessage());
        }
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        
        try {
            String name = request.getParameter("name");
            // 如果name为空，则查询所有学生
            List<Map<String, Object>> students;
            if (name == null || name.trim().isEmpty()) {
                students = studentService.getAllStudentsWithMajor();
            } else {
                students = studentService.getStudentsByNameWithMajor(name.trim());
            }
            outputStudentTable(students, out);
        } catch (Exception e) {
            e.printStackTrace();
            response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            out.println("查询失败：" + e.getMessage());
        }
    }

    private void outputStudentTable(List<Map<String, Object>> students, PrintWriter out) {
        StringBuilder html = new StringBuilder();
        for (Map<String, Object> student : students) {
            html.append("<tr>");
            html.append("<td>").append(student.get("id")).append("</td>");
            html.append("<td>").append(student.get("student_id")).append("</td>");
            html.append("<td>").append(student.get("name")).append("</td>");
            html.append("<td align=\"right\">").append(student.get("age")).append("</td>");
            html.append("<td>").append(student.get("major_name")).append("</td>");
            html.append("<td>").append(student.get("phone") != null ? student.get("phone") : "").append("</td>");
            html.append("<td><a href=\"#\" class=\"edit-link\" data-id=\"").append(student.get("id")).append("\">编辑</a> <a href=\"#\" class=\"delete-link\" data-id=\"").append(student.get("id")).append("\">删除</a></td>");
            html.append("</tr>");
        }
        html.append("<tr>");
        html.append("<td colspan=\"7\" align=\"center\">共").append(students.size()).append("人</td>");
        html.append("</tr>");
        out.println(html.toString());
    }
} 